package com.byedu.course.filter;

import com.byedu.course.util.FocusMailUtil;
import com.byedu.domain.focus.FocusNoticeDO;
import com.byedu.domain.focus.FocusProjectDO;
import com.byedu.domain.focus.FocusUserDO;
import com.byedu.domain.focus.vo.enumvos.FocusStatusTypeEnum;
import com.byedu.domain.focus.vo.FocusUserVO;
import com.byedu.focus.FocusProjectService;
import com.byedu.focus.FocusUserService;
import com.byedu.util.DateUtil;
import com.byedu.util.yunpian.util.StringUtil;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.TimerTask;

import javax.servlet.ServletContext;


/**
 * 继承 定时器任务类
 * 
 */
public class MyTask extends TimerTask {

	public MyTask() {
		super();
	}
    private WebApplicationContext ctx;
    private FocusUserService focusUserService;
    private FocusProjectService focusProjectService;
	/** 这个代表3点钟的时候执行任务 */
	private static final int C_SCHEDULE_HOUR = 0;

	private static boolean isRunning = false;

	private ServletContext context = null;

	public MyTask(ServletContext context) {
		this.context = context;
        ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        if(ctx != null){
            focusUserService = (FocusUserService)ctx.getBean("focusUserService");
            focusProjectService = (FocusProjectService)ctx.getBean("focusProjectService");

        }else{
            System.out.println("获取应用程序上下文失败!");
            return;
        }
	}

	public void run() {
		Calendar cal = Calendar.getInstance();
		System.out.println("执行了");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		String dateStr=sdf.format(System.currentTimeMillis());
        int weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
		System.out.println(dateStr);
//		DataSourceNews.getInstance().updatenotice(dateStr);
//		DataSourceNewst.getInstance().updatenotice(dateStr);
		if (!isRunning) {
				isRunning = true;
				context.log("开始执行指定任务");
            FocusNoticeDO focusNoticeDO = focusUserService.getFocusNotice();
            List<FocusUserVO> focusUserVOList = focusUserService.getFocusUserList();
            //自动变化
            List<FocusProjectDO> focusProjectDOList = focusProjectService.getFocusProjectDOList();
//            for(FocusProjectDO focusProjectDO: focusProjectDOList){

//                //回款超期自动变化  判断开票时间是否超过60天,判断开票时间是否为空,是否为P8
//                if(!StringUtil.isNullOrEmpty(focusProjectDO.getRertime())
//                        && (Integer.parseInt(focusProjectDO.getStatus())== FocusStatusTypeEnum.PO9.getCode()) ){
//                    focusProjectDO.setStatus("9");
//                    focusProjectService.updateFocusProjectDObyId(focusProjectDO);
//                }
//                //回款超期自动变化  判断开票时间是否超过60天,判断开票时间是否为空,是否为P8
//                if(DateUtil.compareDateWithToday(DateUtil.plusDay(focusNoticeDO.getHuikuan(),focusProjectDO.getBilltime()))
//                        && StringUtil.isNullOrEmpty(focusProjectDO.getRertime())
//                        && (Integer.parseInt(focusProjectDO.getStatus())== FocusStatusTypeEnum.PO8.getCode()) ){
//                    focusProjectDO.setStatus("8");
//                    focusProjectService.updateFocusProjectDObyId(focusProjectDO);
//                }
//                //回款超期自动变化  判断开票时间是否超过60天,判断开票时间是否为空,是否为P6
//                if( !StringUtil.isNullOrEmpty(focusProjectDO.getRealcomoutnum())
//                        && (Integer.parseInt(focusProjectDO.getStatus())== FocusStatusTypeEnum.PO7.getCode()) ){
//                    focusProjectDO.setStatus("7");
//                    focusProjectService.updateFocusProjectDObyId(focusProjectDO);
//                }
//                //填支出
//                if(DateUtil.compareDateWithToday(DateUtil.plusDay(focusNoticeDO.getZhichu(),focusProjectDO.getBilltime()))
//                        && StringUtil.isNullOrEmpty(focusProjectDO.getBillnum())
//                        && (Integer.parseInt(focusProjectDO.getStatus())== FocusStatusTypeEnum.PO6.getCode()) ){
//                    focusProjectDO.setStatus("6");
//                    focusProjectService.updateFocusProjectDObyId(focusProjectDO);
//                }
//                if(StringUtil.isNullOrEmpty(focusProjectDO.getPo())
//                        && (Integer.parseInt(focusProjectDO.getStatus())== FocusStatusTypeEnum.PO4.getCode()) ){
//                    focusProjectDO.setStatus("4");
//                    focusProjectService.updateFocusProjectDObyId(focusProjectDO);
//                }
//                if(StringUtil.isNullOrEmpty(focusProjectDO.getTc())
//                        && (Integer.parseInt(focusProjectDO.getStatus())== FocusStatusTypeEnum.PO3.getCode()) ){
//                    focusProjectDO.setStatus("3");
//                    focusProjectService.updateFocusProjectDObyId(focusProjectDO);
//                }
//            }


            if( weekDay == 0 &&focusNoticeDO.getOpennotice() == 0){
                for(int i =0 ;i< focusUserVOList.size();i++){

                    List sizeList = focusProjectService.getFocusUnfinishedProjectVOListByTimeandprid(focusUserVOList.get(i).getId(), null, null, null, null, null, focusUserVOList.get(i).getPermission().getCode());
                    if(sizeList !=null){
                        focusUserVOList.get(i).setUf(sizeList.size());
                        focusUserService.updateFocusUserUserId( new FocusUserDO(focusUserVOList.get(i)));
                    }
                    //周一了 发邮件
    //                weekDay == 1 &&

                        FocusMailUtil.mailsend(focusUserVOList.get(i));
                    }
            }




				// new YouCode().changeState();

				isRunning = false;
				context.log("指定任务执行结束");
		} else {
			context.log("上一次任务执行还未结束");
		}
	}

}